1 00:00:00,480 --> 00:00:01,110 All right. 2 00:00:01,110 --> 00:00:06,300 The next trap that we can work on are going to be the landmines that will explode when a player touches 3 00:00:06,300 --> 00:00:06,810 them. 4 00:00:06,840 --> 00:00:10,110 The models for the landmines are very simple. 5 00:00:10,110 --> 00:00:12,090 We can go ahead and take a look at one of them. 6 00:00:12,090 --> 00:00:14,700 For example, in RB model number eight. 7 00:00:15,090 --> 00:00:18,840 And if we go find that RB model, as you can see here's one of our landmines. 8 00:00:18,840 --> 00:00:22,140 It's just two parts with an invisible part on top of it. 9 00:00:22,140 --> 00:00:24,990 And we're going to check when a player touches this invisible part. 10 00:00:24,990 --> 00:00:27,660 And when they do we're going to make the landmine explode. 11 00:00:28,320 --> 00:00:31,830 So I'll parent this back into replicated storage. 12 00:00:32,690 --> 00:00:36,680 And we're just going to follow the same process that we did for the other traps. 13 00:00:36,680 --> 00:00:41,990 We'll create a new module script inside of starter player scripts in the handler section. 14 00:00:43,360 --> 00:00:46,720 I'm going to call this script my landmine handler. 15 00:00:47,860 --> 00:00:52,630 And we'll paste in that exact same template that we've used before, and I'll rename this service to 16 00:00:52,630 --> 00:00:54,790 be my land mine handler. 17 00:00:55,450 --> 00:01:00,010 We're only going to need an initialize function as well as the collection service. 18 00:01:06,400 --> 00:01:10,240 We can go ahead and define the tag for the service. 19 00:01:10,300 --> 00:01:13,600 So that's going to be the tag of landmine. 20 00:01:14,340 --> 00:01:19,170 And then inside of our initialize function, we'll refer to the collection service get instance added 21 00:01:19,170 --> 00:01:24,630 signal, pass our tag here and connect a lambda function to this to get that instance. 22 00:01:24,630 --> 00:01:28,560 And then we can go ahead and create another module script that's going to be our class. 23 00:01:28,560 --> 00:01:31,500 We'll call this our Land Mine class. 24 00:01:32,510 --> 00:01:34,970 And we'll be using the same class template as before. 25 00:01:34,970 --> 00:01:36,260 We'll paste that in there. 26 00:01:36,260 --> 00:01:42,950 I'll rename this to our Land Mine class and I'll rename this to Land Mine Objects. 27 00:01:43,730 --> 00:01:46,970 And now we can go ahead and require our landmine class. 28 00:01:46,970 --> 00:01:52,580 So landmine class is equal to require script dot landmine class. 29 00:01:54,320 --> 00:01:57,170 And then we can go ahead and use the constructor. 30 00:01:59,600 --> 00:02:04,520 All right inside of the class, we can go ahead and grab the player service to get a reference to our 31 00:02:04,520 --> 00:02:05,360 local player. 32 00:02:05,360 --> 00:02:11,810 And then we can also get the sound service to grab the explosion sound for our land mind. 33 00:02:12,950 --> 00:02:16,160 And then we can go ahead and make a reference to those two things. 34 00:02:16,160 --> 00:02:19,790 So local player is going to be equal two players local player. 35 00:02:19,790 --> 00:02:27,230 And then we can get our explosion sound that's equal to sound service dot SFX dot explosion. 36 00:02:28,110 --> 00:02:34,920 And then we can also define a constant for how long we should wait before responding the landmine so 37 00:02:34,920 --> 00:02:36,360 we can explode the landmine. 38 00:02:36,360 --> 00:02:40,740 We'll wait a set period of time and then make the landmine explode again. 39 00:02:40,740 --> 00:02:43,350 So we can just call this respawn duration. 40 00:02:43,970 --> 00:02:46,430 And set it to something like five seconds. 41 00:02:47,780 --> 00:02:52,340 And then inside of our constructor, we can go ahead and get a reference to the model. 42 00:02:52,760 --> 00:02:55,760 And then we can also store a boolean. 43 00:02:55,760 --> 00:02:58,430 We'll call on Cool Down to be our Debounce. 44 00:02:58,700 --> 00:03:04,310 And then we'll listen to when that detect zone part inside of our landmine gets touched. 45 00:03:06,020 --> 00:03:11,480 And we'll connect a function, get that part that touched it, and then we'll check if that's the player. 46 00:03:11,480 --> 00:03:16,970 So we'll use player's get player from character pass other part dot parent. 47 00:03:17,860 --> 00:03:24,550 And if we do not have a player, then we're just going to return. 48 00:03:25,700 --> 00:03:28,580 Otherwise we can go ahead and explode the landmines. 49 00:03:28,580 --> 00:03:30,830 So we'll create a dedicated function for that. 50 00:03:31,250 --> 00:03:32,840 We'll just call it explode. 51 00:03:32,840 --> 00:03:38,630 And then we can have another function to restore the landmine back to its original state. 52 00:03:38,630 --> 00:03:41,420 And then we'll call self dot explode right here. 53 00:03:42,980 --> 00:03:46,400 And inside of this function, we want to make sure that we're not on our cooldown. 54 00:03:46,400 --> 00:03:48,650 If we are, then we're going to return. 55 00:03:48,710 --> 00:03:51,440 Otherwise, we'll set the cooldown equal to true. 56 00:03:52,230 --> 00:03:55,350 And then what we could do is we could create a new explosion instance. 57 00:03:55,350 --> 00:04:00,390 So I'll call this explosion that's equal to instance dot new explosion. 58 00:04:01,050 --> 00:04:03,870 And then we'll also clone our explosion sound. 59 00:04:07,730 --> 00:04:13,730 And then we'll set play on remove equal to true as well, and then set the parent of this sound equal 60 00:04:13,730 --> 00:04:14,660 to. 61 00:04:14,690 --> 00:04:19,490 We can set it equal to the primary part of our landmine. 62 00:04:19,490 --> 00:04:24,560 So our landmine has a primary part set to it which is one of the parts inside of the landmine. 63 00:04:24,890 --> 00:04:31,190 So we'll do self dot model and get the model's primary part. 64 00:04:31,580 --> 00:04:35,480 And then we can go ahead and set some of the properties for our explosion instance. 65 00:04:35,480 --> 00:04:40,040 So for the explosions blast radius we could do something like 20 studs. 66 00:04:40,040 --> 00:04:45,980 We'll set the explosions blast pressure equal to something like we could just do like 10,000. 67 00:04:45,980 --> 00:04:47,030 It doesn't really matter. 68 00:04:47,710 --> 00:04:52,000 The explosion dot destroy joint radius percent. 69 00:04:52,000 --> 00:04:55,450 We're going to set that to zero to not destroy the player's joints. 70 00:04:55,450 --> 00:05:01,420 And then the position of this explosion in the workspace is going to be equal to our self dot model 71 00:05:01,420 --> 00:05:02,590 dot primary part. 72 00:05:02,590 --> 00:05:06,670 And we're going to get that position of the primary part for our landmine. 73 00:05:07,730 --> 00:05:13,220 And before we apparent this explosion to the workspace, we want to listen to the explosions, hit event, 74 00:05:13,340 --> 00:05:16,310 connect a function, and get that hit part. 75 00:05:17,930 --> 00:05:22,250 And we want to make sure that this hip part belongs to our local player. 76 00:05:22,550 --> 00:05:24,920 So we'll create a variable. 77 00:05:24,920 --> 00:05:26,360 We'll call it hip player. 78 00:05:27,510 --> 00:05:29,760 And that's going to be equal to players. 79 00:05:29,760 --> 00:05:35,580 Get player from character and pass hit part dot parent. 80 00:05:36,300 --> 00:05:41,130 And if this hit player variable is not equal to our local player. 81 00:05:41,130 --> 00:05:45,300 So if it's a nil or if it's somehow another player then we're just going to return. 82 00:05:45,810 --> 00:05:52,050 Otherwise we can go ahead and refer to our local player, get our character and our humanoid, and we 83 00:05:52,050 --> 00:05:59,760 can go ahead and change the state on our humanoid so we can do change state to the enum dot humanoid 84 00:05:59,760 --> 00:06:01,740 state type dot dead. 85 00:06:01,740 --> 00:06:04,620 So this will kill our player's character. 86 00:06:05,480 --> 00:06:11,060 After we've done that, then we can go ahead and parent the explosion equal to, um, the workspace. 87 00:06:11,060 --> 00:06:14,600 Or we could set it equal to the self dot model. 88 00:06:15,020 --> 00:06:17,120 So that means the explosion will happen. 89 00:06:17,120 --> 00:06:20,300 And at the same time we want to go ahead and destroy the sound instance. 90 00:06:20,300 --> 00:06:22,070 So it plays the explosion sound. 91 00:06:22,400 --> 00:06:26,720 And once we do that we also want to I guess make our landmine disappear. 92 00:06:26,720 --> 00:06:29,090 So we could do self dot model. 93 00:06:29,450 --> 00:06:32,900 The first part of our landmine is called top. 94 00:06:32,900 --> 00:06:35,660 And we'll just set the transparency of that equal to one. 95 00:06:35,660 --> 00:06:39,590 And then the other part of our landmine is called the body. 96 00:06:39,590 --> 00:06:43,160 And we'll set that transparency equal to one as well. 97 00:06:43,880 --> 00:06:47,630 And then we can go ahead and delay a function for that respawn duration. 98 00:06:47,630 --> 00:06:50,510 And that's where we would call our restore function. 99 00:06:50,510 --> 00:06:54,440 So we'll pass self dot restore and then pass self. 100 00:06:56,040 --> 00:07:01,620 And then inside of this function, we'll check if our landmine wasn't destroyed. 101 00:07:01,620 --> 00:07:08,160 So if we don't have a reference to the landmine in our self table, meaning we have cleaned up, um, 102 00:07:08,160 --> 00:07:17,670 this particular object, or if the model's parent is equal to nil, then we're just going to return. 103 00:07:17,940 --> 00:07:20,220 Otherwise we can just copy this right here. 104 00:07:20,220 --> 00:07:22,380 Set the transparency to be opaque. 105 00:07:23,050 --> 00:07:27,760 And then set self dot on cool down equal to false. 106 00:07:29,030 --> 00:07:34,670 And then we could also go ahead and clean up the previous explosion instance that's parented inside 107 00:07:34,670 --> 00:07:36,350 of our model, if it exists. 108 00:07:36,350 --> 00:07:40,040 So we'll create a variable, I'll call it explosion. 109 00:07:40,040 --> 00:07:46,850 And that's going to be equal to self dot model find first child which is a. 110 00:07:47,970 --> 00:07:49,410 Explosion. 111 00:07:50,260 --> 00:07:54,400 So if we found an explosion, then we can just go ahead and destroy it. 112 00:07:54,850 --> 00:07:59,680 And then I'll just move this above where we set cooldown equal to false. 113 00:07:59,710 --> 00:08:05,860 So now any time a landmine gets touched in our game, we should be able to blow it up, play an explosion 114 00:08:05,860 --> 00:08:06,340 sound. 115 00:08:06,340 --> 00:08:10,870 And if our player is within range of that explosion, then our player should die. 116 00:08:11,460 --> 00:08:12,930 So I think that's all we need to do. 117 00:08:12,930 --> 00:08:16,170 Let's go ahead and test out to see if this is working. 118 00:08:16,320 --> 00:08:17,490 We'll play the game. 119 00:08:18,610 --> 00:08:23,950 And we get no errors in the console, which is always good, but that doesn't mean any landmines spawn, 120 00:08:23,950 --> 00:08:27,280 so let's keep a keen eye out for any landmines. 121 00:08:28,680 --> 00:08:29,280 Okay. 122 00:08:29,280 --> 00:08:30,900 Here I found one of the landmines. 123 00:08:30,900 --> 00:08:33,960 And if I go and touch it, hopefully it should explode and kill me. 124 00:08:33,960 --> 00:08:36,090 So if I touch it, there we go. 125 00:08:36,090 --> 00:08:37,590 Explosion sound. 126 00:08:37,590 --> 00:08:46,590 My character dies and we move back to where we were at, and it looks like it has successfully respawn. 127 00:08:46,590 --> 00:08:48,210 So if I touch it again, there we go. 128 00:08:48,210 --> 00:08:50,280 It blows up, I die. 129 00:08:51,330 --> 00:08:53,190 And then we come back. 130 00:08:53,400 --> 00:08:56,070 Let me see if we can see the parts actually disappear. 131 00:08:56,070 --> 00:08:57,600 So if I touch it one more time. 132 00:08:58,230 --> 00:09:00,330 Yep, those parts disappeared. 133 00:09:00,330 --> 00:09:06,600 And then five seconds later, the landmine responds itself and it continues to blow us up. 134 00:09:07,200 --> 00:09:08,370 Very cool. 135 00:09:09,740 --> 00:09:14,480 We have one more trap to script and that's going to be done in the next lecture. 136 00:09:14,480 --> 00:09:15,530 See you there.